/******************************************************************************
*filename: dstruct.h
*Holds the seeder,leecher and file_part data structures.
*This file must be included in both the sparta torrent program and the tracker.
*as well.
*part-size = 64kb.(pieces) (65536)
*
*Author : Neeraj Naik
*Date:4th Nov 2010
*******************************************************************************/

#ifndef DSTRUCT
#define DSTRUCT
#include "header.h"
#define MAX_SEEDS 10
#define MAX_LEECHES 10
#define PARTSIZE 65536
#define NUMBER_OF_THREADS 10
struct seeder_args{
	
	int socket;
	struct file_part *head;
	
}seed_arguments[NUMBER_OF_THREADS];

struct seeder{
	int seeder_id;
	struct sockaddr_in sd_sin;
};

struct leecher{
	int leecher_id;
	struct sockaddr_in lc_sin;
};

// file_part is a linked list.
struct file_part{

    char filename[MAX_FILENAME_LEN];		//name of the file to which the part belongs.
	int part_num;		//part number (can be used for somethng like hash checking)
	
	FILE *data_ptr;		//ptr to the file created for this part.ie whr the data of this part is stored.
				//file descriptor for FILE* data_ptr.
	int file_size;		//Number of bytes (part size)
	
//	int offset;		//will hold the offset to the starting point of the part. each part being 64kb.
		
	struct seeder seeder_list[MAX_SEEDS];	//list of seeders.
	struct leecher leecher_list[MAX_LEECHES];	//
	
	float percent_downloaded;
	enum status{SEEDING,COMPLETE,LEECHING,PENDING}download_status;
	struct file_part *next;
		
};

void create_list(struct file_part **headref,char * filename ,int pieces, int piecesize, int lpsize,int peer_id);
//enum check_file(struct file_part *,enum ,int );
FILE* join_file(struct file_part *head);//once all parts are downloaded, this fn will join and make a complete file.after downloading, this function 						will first cross check 'filename' & 'part_num' to see if file is in order.
void print_list(struct file_part *head);//might be needed while debugging.
static float calc_percent(struct file_part* );// HELPER FUNCTION:calculates the data in a file part
int random_select(int max_pieces);
void leecher_client(struct file_part *head,char *filename,int part_num);
void seeder_server(int sock,struct file_part *head);
// Functions used by seeder
void main_seeder(struct file_part *head);
void initial_tracker_update(char *trackerIP,unsigned int trackerport,char * filename, int pieces,int peer_id);
void info_req2track_populate(char *trackerIP,unsigned int trackerport,char * filename, int pieces,int peer_id,struct file_part *head);
//Functions used by leecher


#endif	//DSTRUCT
